package cn.hh.wechatkit.pojo.message.imp.commumication;


import cn.hh.wechatkit.pojo.message.imp.Wx_BaseMsg;
import cn.hhchat.kit.xml.XmlUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.Date;
import java.util.Map;

/**
 * Created this one by HMH on 2017/3/24.
 */

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Wx_TextMsg extends Wx_BaseMsg {

    private String Content="";

    public Wx_TextMsg(Map<String, String> map) {
        super(map);
        this.Content = map.get("Content");
    }

    @JsonIgnore
    public String MsgToXml() {
        //生成xml文档
        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding("utf-8");
        Element root = document.addElement("xml");
        //构造 ToUserName
        Element ToUserName = root.addElement("ToUserName")
                .addText(XmlUtil.genCdataAttr(this.getToUserName()));
        //添加 FromUserName
        Element FromUserName = root.addElement("FromUserName")
                .addText(XmlUtil.genCdataAttr(this.getFromUserName()));
        //构造 CreateTime
        Element createTime = root.addElement("CreateTime")
                .addText(XmlUtil.genCdataAttr(String.valueOf(this.getCreateTime())));
        //构造 MsgType
        Element msgType = root.addElement("MsgType")
                .addText(XmlUtil.genCdataAttr("text"));
        //构造 Content
        Element Content = root.addElement("Content")
                .addText(XmlUtil.genCdataAttr(this.Content));
        return cn.hh.wechatkit.util.XmlUtil.formatXml(document, "utf-8", false);
    }

    @JsonIgnore
    String messageTemplate = "<xml>\n" +
            " <ToUserName><![CDATA[{ToUserName}]]></ToUserName>\n" +
            " <FromUserName><![CDATA[{FromUserName}]]></FromUserName>\n" +
            " <CreateTime>{CreateTime}</CreateTime>\n" +
            " <MsgType><![CDATA[{MsgType}]]></MsgType>\n" +
            " <Content><![CDATA[{Content}]]></Content>\n" +
            " <MsgId>{MsgId}</MsgId>\n" +
            " </xml>";
}
